package com.oazon.moco.playlist.db;

import java.util.ArrayList;
import java.util.List;

import com.oazon.common.Logger;
import com.oazon.moco.bean.Metadata;
import com.oazon.moco.bean.Metadata.Res;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PlaylistDao {
	private SQLiteDatabase db;

	private static final String TABLE_NAME = "play_list";
	
	private static final String ID = "id";
	private static final String SERVICE_ID = "service_id";
	private static final String ITEM_ID = "item_id";
	private static final String PARENT_ID = "parent_id";
	private static final String RESTRICTED = "restricted";
	private static final String STREAM_CONTENT = "stream_content";
	private static final String RADIA_SHOW_MD = "radio_show_md";
	private static final String ALBUM_ART_URI = "album_art_uri";
	private static final String TITLE = "title";
	private static final String OBJECT_CLASS = "object_class";
	private static final String CREATE = "creator";
	private static final String ALBUM = "album";
	private static final String LYRIC_URI = "lyric_uri";
	private static final String IS_AUTO_ADD = "is_auto_add";
	private static final String IS_FAVORITE ="is_favorite";
	private static final String PROTOCOL_INFO ="protocol_info";
	private static final String DURATION ="duration";
	private static final String VALUE ="value";

	

	public PlaylistDao(Context context) {
		MocoDB playListDB = new MocoDB(context);
		db = playListDB.getReadableDatabase();
	}
	
	public synchronized void saveAllPlayList(List<Metadata> metadatas){
		deleteAll();
		for (Metadata metadata : metadatas) {
			try{
				savePlayList(metadata);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
	
	public synchronized long savePlayList(Metadata metadata){
		if (metadata.getId() == null)
			return -1;
		Logger.d("wwww", "savePlayList================================\r\n====================");
		ContentValues values = new ContentValues();
		values.put(ID, metadata.getId());
		deleteRepeatById(metadata.getId());
		
		values.put(SERVICE_ID, metadata.getServiceID());
		values.put(ITEM_ID, metadata.getItemID());
		values.put(PARENT_ID, metadata.getParentID());
		values.put(RESTRICTED, metadata.isRestricted());
		values.put(STREAM_CONTENT, metadata.getStreamContent());
		values.put(RADIA_SHOW_MD, metadata.getRadioShowMd());
		values.put(ALBUM_ART_URI, metadata.getAlbumArtURI());
		values.put(TITLE, metadata.getTitle());
		values.put(OBJECT_CLASS, metadata.getObjectClass());
		values.put(CREATE, metadata.getCreator());
		values.put(ALBUM, metadata.getAlbum());
		values.put(LYRIC_URI, metadata.getLyricURI());
		values.put(IS_AUTO_ADD, metadata.isAutoAdd());
		values.put(IS_FAVORITE, metadata.isFavorite());
		values.put(PROTOCOL_INFO, metadata.getRes().getProtocolInfo());
		values.put(DURATION, metadata.getRes().getDuration());
		values.put(VALUE, metadata.getRes().getValue());
		
		return db.insert(TABLE_NAME, null, values);
	}
	
	private synchronized long deleteRepeatById(String id) {
		if (id == null || id.equals(""))
			return -1;
		//Log.d("wowdd1", "delete " + id);
		
		return db.delete(TABLE_NAME, ID+"=?", new String[]{ id });
	}
	
	public synchronized int deleteAll(){
		return db.delete(TABLE_NAME, null, null);
	}
	
	public List<Metadata> getAll(){
		
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		ArrayList<Metadata> metadatas = new ArrayList<Metadata>();

		while (cursor.moveToNext()) {
			Metadata metadata = new Metadata();
			metadata.setId(cursor.getString(cursor.getColumnIndex(ID)));
			metadata.setServiceID(cursor.getString(cursor.getColumnIndex(SERVICE_ID)));
			metadata.setItemID(cursor.getString(cursor.getColumnIndex(ITEM_ID)));
			metadata.setParentID(cursor.getString(cursor.getColumnIndex(PARENT_ID)));
			metadata.setRestricted(Boolean.valueOf(cursor.getString(cursor.getColumnIndex(RESTRICTED))));
			metadata.setStreamContent(cursor.getString(cursor.getColumnIndex(STREAM_CONTENT)));
			metadata.setRadioShowMd(cursor.getString(cursor.getColumnIndex(RADIA_SHOW_MD)));
			metadata.setAlbumArtURI(cursor.getString(cursor.getColumnIndex(ALBUM_ART_URI)));
			metadata.setTitle(cursor.getString(cursor.getColumnIndex(TITLE)));
			metadata.setObjectClass(cursor.getString(cursor.getColumnIndex(OBJECT_CLASS)));
			metadata.setCreator(cursor.getString(cursor.getColumnIndex(CREATE)));
			metadata.setAlbum(cursor.getString(cursor.getColumnIndex(ALBUM)));
			metadata.setLyricURI(cursor.getString(cursor.getColumnIndex(LYRIC_URI)));
			metadata.setAutoAdd(Boolean.valueOf(cursor.getString(cursor.getColumnIndex(IS_AUTO_ADD))));
			metadata.setFavorite(Boolean.valueOf(cursor.getString(cursor.getColumnIndex(IS_FAVORITE))));
			Res res = new Res();
			res.setProtocolInfo(cursor.getString(cursor.getColumnIndex(PROTOCOL_INFO)));
			res.setDuration(cursor.getString(cursor.getColumnIndex(DURATION)));
			res.setValue(cursor.getString(cursor.getColumnIndex(VALUE)));
			metadata.setRes(res);
			
			metadatas.add(metadata);
		}
		return metadatas;
	}
}
